﻿using System;
using System.Configuration;
using System.Web;
using System.Web.Caching;
using System.Xml;
using System.Collections.Generic;
using System.Xml.Serialization;
using System.IO;

namespace Common
{
    public sealed class XmlDataHerper
    {
        private static string xmlFilePath
        {
            get
            {
                string path = System.Configuration.ConfigurationManager.AppSettings["XMLDataFilePath"];
                if (string.IsNullOrWhiteSpace(path))
                {
                    return "/Content/Resource/XMLDataFile.xml";
                }
                else
                {
                    return path;
                }
            }
        }


        /// <summary>
        /// 取得某一类型的信息集合
        /// </summary>
        /// <param name="typeName"></param>
        /// <returns></returns>
        public static Dictionary<string, string> GetKeyValue(string typeName)
        {
            string catchName = string.Format("XMLData_Type_{0}", typeName);

            object obj = CacheHelper.GetCache(catchName);
            try
            {
                if (obj == null)
                {
                    XmlNode xn = GetXmlNode(typeName);
                    Dictionary<string, string> keyValueList = new Dictionary<string, string>();

                    foreach (XmlNode item in xn.ChildNodes)
                    {
                        string value = GetAttribute(item.Attributes["Value"]);
                        string text = item.InnerText;
                        keyValueList.Add(value, text);
                    }

                    CacheDependency cdp = new CacheDependency(HttpContext.Current.Server.MapPath(xmlFilePath));
                    CacheHelper.SetCache(catchName, keyValueList, cdp);
                    obj = keyValueList;
                }
            }
            catch { }
            return obj as Dictionary<string, string>;
        }

        /// <summary>
        /// 根据值取得文本
        /// </summary>
        /// <param name="typeName"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string GetTextByValue(string typeName, string value)
        {
            Dictionary<string, string> keyValue = GetKeyValue(typeName);
            if (keyValue.ContainsKey(value))
            {
                return keyValue[value];
            }
            else
            {
                return value;
            }
        }

        /// <summary>
        /// 根据文本取得值
        /// </summary>
        /// <param name="typeName"></param>
        /// <param name="text"></param>
        /// <returns></returns>
        public static string GetValueByText(string typeName, string text)
        {
            string value = string.Empty;
            foreach (KeyValuePair<string, string> item in GetKeyValue(typeName))
            {
                if (item.Value == text)
                {
                    value = item.Key;
                    break;
                }
            }
            return value;
        }

        private static string GetAttribute(XmlAttribute attribute)
        {
            if (attribute == null)
            {
                return string.Empty;
            }
            else
            {
                return attribute.Value.Trim();
            }
        }
        /// <summary>
        /// 取得一类型的节点
        /// </summary>
        /// <param name="typeName"></param>
        /// <returns></returns>
        private static XmlNode GetXmlNode(string typeName)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(HttpContext.Current.Server.MapPath(xmlFilePath));
            string node = string.Format("/Root/DataList[@Type='{0}']", typeName);
            XmlNode xmlNode = doc.SelectSingleNode(node);
            return xmlNode;
        }

        /// <summary>
        /// 根据xml字符串返回Model
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="xmlObj"></param>
        /// <returns></returns>
        public static T DeserializeXML<T>(string xmlObj)
        {
            XmlSerializer serializer = new XmlSerializer(typeof(T));
            using (StringReader reader = new StringReader(xmlObj))
            {
                return (T)serializer.Deserialize(reader);
            }
        }  
    }
}